package online.afeibaili.storebackendframework.repository

import online.afeibaili.storebackendframework.model.ProductEntity
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.stereotype.Repository


/**
 * 商品存储类
 *
 *@author AfeiBaili
 *@version 2025/10/17 16:01
 */

@Repository
interface ProductRepository : JpaRepository<ProductEntity, Long> {
    fun deleteByCreateTime(ctId: Long): ProductEntity?
    fun findByCategory(category: String): List<ProductEntity>
    fun findByName(name: String): ProductEntity?
    fun findByPrice(price: Double): List<ProductEntity>
    fun findByPriceBetween(min: Double, max: Double, pageable: Pageable): Page<ProductEntity>
    fun findByCreateTimeIn(ids: List<Long>): List<ProductEntity>

    fun findAllBy(pageable: Pageable): Page<ProductEntity>
    fun findAllByOrderBySalesDesc(pageable: Pageable): Page<ProductEntity>
    fun findAllByOrderByPriceDesc(pageable: Pageable): Page<ProductEntity>
    fun findByNameContainingOrDetailContaining(field: String, field2: String, pageable: Pageable): Page<ProductEntity>

    @Query("select p from ProductEntity p where p.isPoints = :isPoints")
    fun findByIsPoints(@Param("isPoints") boolean: Boolean, pageable: Pageable): List<ProductEntity>

    @Query(value = "SELECT * FROM product ORDER BY RAND() LIMIT :count", nativeQuery = true)
    fun findRandom(@Param("count") count: Int): MutableList<ProductEntity>
}